package com.comet.controller;

import com.comet.domain.Course;
import com.comet.service.CourseService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.HashMap;

@RestController
@RequestMapping("/api/v1/video/")
public class CourseController {

    @Resource
    private CourseService courseService;

    // 扣减库存
    @RequestMapping("deduct_stock_by_sku_id")
    public int deductStockBySkuId(int skuId, Integer num) {
        return courseService.deductStockBySkuId(skuId, num);
    }

    // http://localhost:8888/order-service/addCourse
    @RequestMapping("addCourse")
    public Object addCourse() {
        for (int i = 0; i < 10; i++) {
            Course course = new Course();
            course.setCourseName("SpringCloud从入门到精通");
            course.setCourseDesc("玩转SpringCloud，就在这里");
            course.setPrice(new BigDecimal(399.00));
            course.setStatus(0);
            course.setLogoUrl("/img/course_logo/sb.jpg");
            course.setCategory("Java开发");
            course.setRemark("无备注");
            courseService.addCourse(course);
        }
        return new HashMap<>().put("Status","Success");
    }


    /**
     * 根据视频ID查询课程
     * @param videoId
     * @param request
     * @URL localhost:9000/api/v1/video/find_video_by_id?videoId=41
     */
    @RequestMapping("find_video_by_id")
    public Course findVideoById(int videoId, HttpServletRequest request) {
        Course course = courseService.findById(videoId);
        return course;
    }

    /**
     * 插入一个课程
     * @param course
     * @URL localhost:9000/api/v1/video/save_video
     */
    @PostMapping("save_video")
    public int saveVideo(@RequestBody Course course){
        int effectLine = courseService.saveVideo(course);
        return effectLine;
    }

}
